package com.ldg.shop.controller;

import com.ld.utils.PageUtils;
import com.ld.pojo.R;
import com.ldg.shop.group.Specification;
import com.ldg.shop.service.SpecificationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Liudao
 * @Date: 2022-09-16-16:28
 * @Description:
 */
@RestController
@RequestMapping("specification")
public class SpecificationController {

    @Resource
    private SpecificationService specificationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);

        return R.ok().put("page", page);
    }
    @GetMapping("/findAll")
    public R findAll(){
        return  R.ok().put("list",specificationService.list());
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        Specification specification = specificationService.findById(id);

        return R.ok().put("specification", specification);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Specification specification){
        System.out.println("specification = " + specification);
        specificationService.save(specification);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody Specification specification){
        specificationService.update(specification);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids){
        specificationService.deleteAll(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据模板id查询规格及规格选项列表
     * @param id
     * @return
     */
    @GetMapping("findSpecList/{id}")
    public R findSpecList(@PathVariable String id){
        List<Map> specifications = specificationService.findSpecList(id);
        return R.ok().put("list",specifications);
    }

}
